//
//  ViewController.m
//  ScrollviewDemo
//
//  Created by zhouLiHuan on 2017/1/4.
//  Copyright © 2017年 zhouLiHuan. All rights reserved.
//

#import "ViewController.h"
#import "UINavigationBar+LH.h"

#define kScreenHeight CGRectGetHeight([UIScreen mainScreen].bounds)
@interface ViewController () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic) CGFloat halfHeight;
@property (nonatomic,strong)UILabel *titlelabel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController.navigationBar lhSetBackgroundColor:[UIColor clearColor]];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,64, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background"]];
    
    [self.view addSubview:self.tableView];
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    self.titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    self.titlelabel.alpha = 1.0;
    self.titlelabel.textAlignment = NSTextAlignmentCenter;
    self.titlelabel.text = @"标题";
    self.titlelabel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = self.titlelabel;
    
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;//导航栏的背景色是黑色, 字体为白色
    [self scrollViewDidScroll:self.tableView];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];//用于去除导航栏的底线，也就是周围的边线
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tableView.delegate = nil;
    [self.navigationController.navigationBar lhReset];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    UIColor *color = [UIColor colorWithRed:45/255.0 green:45/255.0 blue:45/255.0 alpha:1];
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY >= - self.view.frame.size.height ) {
        CGFloat alpha = 1- offsetY/self.view.frame.size.height;
        [self.navigationController.navigationBar lhSetBackgroundColor:[color colorWithAlphaComponent:alpha]];
        self.titlelabel.alpha = alpha;
    } else {
        [self.navigationController.navigationBar lhSetBackgroundColor:[color colorWithAlphaComponent:0]];
    }
}

#pragma mark UITableViewDatasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"CustomCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = @"text";
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}


@end
